package tt.dz.appserver.mvc.controller;

import java.io.PrintWriter;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.mortbay.log.Log;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;

import tt.dz.appserver.entity.model.ResultMappingEntity;
import tt.dz.appserver.entity.model.pub.station.StationCommentReply;
import tt.dz.appserver.entity.vo.station.StationCommentInfoVo;
import tt.dz.appserver.entity.vo.station.StationCommentReplyInfoVo;
import tt.dz.appserver.service.business.StationService;
import tt.dz.appserver.util.Constants;
import tt.dz.appserver.util.Result;
import tt.dz.appserver.util.enums.Response;


/**
 * 电站控制器
 * @author xielf
 *
 */
@Controller
@RequestMapping(value="/station")
public class StationController {

	
	private static final Logger LOG = Logger.getLogger("StationController:");
	
	/**站点列表接口*/
	@Resource
	private StationService stationService;
	
	/**
	 * 获取站点列表
	 * @return
	 */
	@RequestMapping(value="/getAllStation",produces={"application/json;charset=utf-8"},method=RequestMethod.GET)
	public void stationList(HttpServletResponse response){
		
		ResultMappingEntity resultMappingEntity= null;

		response.setHeader("Content-Type", "application/json;charset=utf-8");
		try {
			//获取所有的站点信息
			resultMappingEntity = stationService.getStationDataPackage();
			
		} catch (Exception e) {
			e.printStackTrace();
			resultMappingEntity = Result.responseBody(Response.RUNTIME_ERROR.getCode(), Response.RUNTIME_ERROR.getMsg(), e.getMessage());
		}
		
		//输出结果
		PrintWriter out = null; 
		try {
			String resultString = resultMappingEntity.toString();
			response.setHeader("Content-Length",resultString.length()+"");
			out = response.getWriter();
			out.print(resultString);
		} catch (Exception e) {
			e.printStackTrace();
		}finally{
			out.close();
		}
	}
	
	/**
	 * 按坐标查询
	 * @param response
	 * @param latitude
	 * @param longtitude
	 * @param number
	 */
	@RequestMapping(value="/getStationByCoordinate",produces={"application/json;charset=utf-8"},method=RequestMethod.GET)
	public void getStationByCoordinate(HttpServletResponse response,String latitude,String longtitude,@RequestParam(required=false , defaultValue="20")Integer number
			,String locaLatitude,String locaLongtitude){
		
		ResultMappingEntity resultMappingEntity= null;

		response.setHeader("Content-Type", "application/json;charset=utf-8");
		try {
			//获取所有的站点信息
			resultMappingEntity = stationService.getStationByCoordinate(latitude,longtitude,number, locaLatitude, locaLongtitude);
			
		} catch (Exception e) {
			e.printStackTrace();
			resultMappingEntity = Result.responseBody(Response.RUNTIME_ERROR.getCode(), Response.RUNTIME_ERROR.getMsg(), e.getMessage());
		}
		
		//输出结果
		PrintWriter out = null; 
		try {
			String resultString = resultMappingEntity.toString();
			response.setHeader("Content-Length",resultString.length()+"");
			out = response.getWriter();
			out.print(resultString);
		} catch (Exception e) {
			e.printStackTrace();
		}finally{
			out.close();
		}
	}
	
	
	/**
	 * 按地址查询
	 * @param response
	 * @param name
	 * @param cityCode
	 */
	@RequestMapping(value="/getStationByName",produces={"application/json;charset=utf-8"},method=RequestMethod.GET)
	public void getStationByName(@RequestParam(required=true)String name,@RequestParam(required=false,defaultValue="")String cityCode,String latitude,String longtitude,HttpServletResponse response){
		
		ResultMappingEntity resultMappingEntity= null;

			String sss=response.getHeader("Content-Type");
		response.setHeader("Content-Type", "application/json;charset=utf-8");
		try {
			//获取所有的站点信息
			resultMappingEntity = stationService.getStationByName(name,cityCode,latitude,longtitude);
			
		} catch (Exception e) {
			e.printStackTrace();
			resultMappingEntity = Result.responseBody(Response.RUNTIME_ERROR.getCode(), Response.RUNTIME_ERROR.getMsg(), e.getMessage());
		}
		
		//输出结果
		PrintWriter out = null; 
		try {
			String resultString = resultMappingEntity.toString();
			response.setHeader("Content-Length",resultString.length()+"");
			out = response.getWriter();
			out.print(resultString);
		} catch (Exception e) {
			e.printStackTrace();
		}finally{
			out.close();
		}
	}
	
	/**
	 * 更新站点数据包
	 * @param provinceId
	 * @return
	 */
	@RequestMapping(value="/updateStationDataPackage",produces={"application/json;charset=utf-8"},method=RequestMethod.PUT)
	public void updateStationDataPackage(
			@RequestParam(value="provinceId", required=true) String provinceId,
			HttpServletResponse response){
		
		ResultMappingEntity resultMappingEntity= null;
		
		try {
			//获取所有的站点信息
			JSONObject resultJsonObject = stationService.updateStationDataPackage(provinceId);
			
			String code = resultJsonObject.getString("code");
			String msg = resultJsonObject.getString("msg");

			response.setHeader("Content-Type", "application/json;charset=utf-8");
			if(Response.SUCCESS.getCode().equals(code)){
				resultJsonObject.remove("code");
				resultJsonObject.remove("msg");
				
				//设置需要的头部信息
				response.setHeader("Content-Encoding", "zip");
				resultMappingEntity = Result.responseBody(code, msg, resultJsonObject);
			}
			else{
				resultMappingEntity = Result.responseBody(code, msg);
			}
		} catch (Exception e) {
			e.printStackTrace();
			resultMappingEntity = Result.responseBody(Response.RUNTIME_ERROR.getCode(), Response.RUNTIME_ERROR.getMsg(), e.getMessage());
		}
		//输出结果
		PrintWriter out = null; 
		try {
			response.setHeader("Content-Length",resultMappingEntity.toString()+"");
			response.setCharacterEncoding(Constants.CHARSET);
			out = response.getWriter();
			out.print(resultMappingEntity.toString());
		} catch (Exception e) {
			e.printStackTrace();
		}finally{
			out.close();
		}
	}
	
	/**
	 * 获取站点详细
	 * @param stationId
	 * @return
	 */
	@RequestMapping(value="/getStationDetail",produces={"application/json;charset=utf-8"},method=RequestMethod.GET)
	@ResponseBody
	public String stationDetail(
			@RequestParam(value="stationId", required=true)  String stationId,
			@RequestParam(value="userId", required=false,defaultValue="")  String userId){
		
		ResultMappingEntity resultMappingEntity= null;
		
		try {
			//获取站点详细
			JSONObject resultJsonObject = stationService.getStationDetailByStationId(stationId, userId);
			String code = resultJsonObject.getString("code");
			String msg = resultJsonObject.getString("msg");
			
			if(Response.SUCCESS.getCode().equals(code)){
				resultJsonObject.remove("code");
				resultJsonObject.remove("msg");
				resultMappingEntity = Result.responseBody(code, msg, resultJsonObject.getJSONObject("stationInfo"));
			}
			else{
				resultMappingEntity = Result.responseBody(code, msg);
			}
		} catch (Exception e) {
			e.printStackTrace();
			resultMappingEntity = Result.responseBody(Response.RUNTIME_ERROR.getCode(), Response.RUNTIME_ERROR.getMsg(), e.getMessage());
		}
		return resultMappingEntity.toString();
	}

	/**
	 * 站点收藏
	 * @param userId
	 * @param stationId
	 * @param stationName
	 * @param latitude
	 * @param longitude
	 * @param address
	 * @return
	 */
	@RequestMapping(value="/collectionStation",produces={"application/json;charset=utf-8"},method=RequestMethod.POST)
	@ResponseBody
	public String collectionStation(
			@RequestParam(value="userId") String userId,
			@RequestParam(value="stationId") String stationId){
		
		ResultMappingEntity resultMappingEntity= null;
		
		try {
			
			JSONObject resultObject = stationService.collectionStation(userId, stationId);
			
			String code = resultObject.getString("code");
			String msg = resultObject.getString("msg");
			
			resultMappingEntity = Result.responseBody(code, msg);
			
		} catch (Exception e) {
			e.printStackTrace();
			resultMappingEntity = Result.responseBody(Response.RUNTIME_ERROR.getCode(), Response.RUNTIME_ERROR.getMsg(), e.getMessage());
		}
		return resultMappingEntity.toString();
	}
	
	/**
	 * 站点收藏列表
	 * @param userId
	 * @param pageNo
	 * @return
	 */
	@RequestMapping(value="/collectionStationList",produces={"application/json;charset=utf-8"},method=RequestMethod.GET)
	@ResponseBody
	public String collectionStationList(
			@RequestParam(value="userId") String userId,@RequestParam(value="pageNo",defaultValue="1") Integer pageNo ){
		
		ResultMappingEntity resultMappingEntity= null;
		
		try {
			
			 resultMappingEntity = stationService.collectionStationList(userId,pageNo);			
			
		} catch (Exception e) {
			e.printStackTrace();
			resultMappingEntity = Result.responseBody(Response.RUNTIME_ERROR.getCode(), Response.RUNTIME_ERROR.getMsg(), e.getMessage());
		}
		return resultMappingEntity.toString();
	}
	
	/**
	 * 电桩评论
	 * @param userId
	 * @param stationId
	 * @return
	 */
	@RequestMapping(value="/stationComment",produces={"application/json;charset=utf-8"},method=RequestMethod.POST)
	@ResponseBody
	public String stationComment(
			@RequestParam(value="userId") String userId,
			@RequestParam(value="stationId") String stationId,
			@RequestParam(value="grade" ,defaultValue="5") Integer grade,
			String address,String content){
		
		ResultMappingEntity resultMappingEntity= null;
		
		try {
			StationCommentInfoVo stationCommentVo = stationService.stationComment(stationId, userId, content, grade, address);
			resultMappingEntity = Result.responseBody(Response.SUCCESS.getCode(), Response.SUCCESS.getMsg(), stationCommentVo);
		} catch (Exception e) {
			e.printStackTrace();
			resultMappingEntity = Result.responseBody(Response.RUNTIME_ERROR.getCode(), Response.RUNTIME_ERROR.getMsg(), e.getMessage());
		}
		//LOG.info("电桩评论返回的结果:"+resultMappingEntity.toString());
		return resultMappingEntity.toString();
	}
	
	/**
	 * 电桩评论列表
	 * @param stationId
	 * @return
	 */
	@RequestMapping(value="/stationCommentList",produces={"application/json;charset=utf-8"},method=RequestMethod.GET)
	@ResponseBody
	public String stationCommentList(
			@RequestParam(value="stationId") String stationId,@RequestParam(defaultValue="1") Integer pageNo,
			@RequestParam(value="userId",defaultValue="") String userId){
		
		LOG.info("点赞评论列表传入参数stationId="+stationId+";pageNo="+pageNo+";userId="+userId);
		ResultMappingEntity resultMappingEntity= null;
		
		try {
			List<StationCommentInfoVo> commentVos = stationService.stationCommentList(stationId, pageNo,userId);
			resultMappingEntity = Result.responseBody(Response.SUCCESS.getCode(), Response.SUCCESS.getMsg(),commentVos);
		} catch (Exception e) {
			e.printStackTrace();
			resultMappingEntity = Result.responseBody(Response.RUNTIME_ERROR.getCode(), Response.RUNTIME_ERROR.getMsg(), e.getMessage());
		}
		//LOG.info("站评论返回结果：【"+resultMappingEntity.toString()+"】");
		return resultMappingEntity.toString();
	}
	/**
	 * 站点评论点赞
	 * @param userId
	 * @param shareId
	 * @return
	 */
	@RequestMapping(value="/stationThumbUp",produces={"application/json;charset=utf-8"},method=RequestMethod.POST)
	@ResponseBody
	public String stationThumbUp(String userId, String commentId){

		ResultMappingEntity resultMappingEntity= null;
		try {
			resultMappingEntity = stationService.stationThumbUp(userId, commentId);
		} catch (Exception e) {
			e.printStackTrace();
			//异常
			resultMappingEntity = Result.responseBody(Response.RUNTIME_ERROR.getCode(), Response.RUNTIME_ERROR.getMsg(), e.getMessage());
		}
		return resultMappingEntity.toString();
	}
	/**
	 * 站点评论回复点赞
	 * @param userId
	 * @param shareId
	 * @return
	 */
	@RequestMapping(value="/stationCommentReplyThumbUp",produces={"application/json;charset=utf-8"},method=RequestMethod.POST)
	@ResponseBody
	public String stationCommentReplyThumbUp(String userId, String replyId){

		ResultMappingEntity resultMappingEntity= null;
		try {
			resultMappingEntity = stationService.stationCommentReplyThumbUp(userId, replyId);
		} catch (Exception e) {
			e.printStackTrace();
			//异常
			resultMappingEntity = Result.responseBody(Response.RUNTIME_ERROR.getCode(), Response.RUNTIME_ERROR.getMsg(), e.getMessage());
		}
		return resultMappingEntity.toString();
	}
	
	/**
	 * 电桩评论回复
	 * @param userId 回复者id
	 * @param commentId 电桩评论id 
	 * @param replyUserId 回复评论的回复
	 * @param content 回复内容
	 * @param address 回复所在地址
	 * @return
	 */
	@RequestMapping(value="/stationCommentReply",produces={"application/json;charset=utf-8"},method=RequestMethod.POST)
	@ResponseBody
	public String stationCommentReply(
			@RequestParam(required=true) String userId, 
			@RequestParam(required=true) String commentId, 
			@RequestParam(required=false) String replyUserId, 
			@RequestParam(required=true) String content, 
			@RequestParam(required=false) String address,
			@RequestParam(required=true) String stationId
			,@RequestParam(required=false ,defaultValue="5")int garde){
		
		ResultMappingEntity resultMappingEntity= null;
		
		try {
			ResultMappingEntity stationCommentVo = stationService.stationCommentReply(userId, commentId, replyUserId, content,address,stationId,garde);
			resultMappingEntity =stationCommentVo;
		} catch (Exception e) {
			e.printStackTrace();
			resultMappingEntity = Result.responseBody(Response.RUNTIME_ERROR.getCode(), Response.RUNTIME_ERROR.getMsg(), e.getMessage());
		}
		//LOG.info("电桩评论返回的结果:"+resultMappingEntity.toString());
		return resultMappingEntity.toString();
	}
	
	@RequestMapping(value = "/stationCommentReplyList", produces = {"application/json;charset=utf-8"}, method = RequestMethod.GET)
	@ResponseBody
	public String stationCommentReplyList(
			@RequestParam(required=true) String commentId,@RequestParam(defaultValue="")String userId,@RequestParam(defaultValue="1") Integer pageNo){
		
		ResultMappingEntity resultMappingEntity = null;
		
		try {
			List<StationCommentReplyInfoVo>  stationcommentreplyList=stationService.getStationReplyByCommentId(commentId,userId,pageNo);
			//resultMappingEntity = shareService.shareComment(userId, shareId, replyUserId, content, commentAddr);
			resultMappingEntity=Result.responseBody(Response.SUCCESS.getCode(), Response.SUCCESS.getMsg(), stationcommentreplyList);
		} catch (Exception e) {
			e.printStackTrace();
			resultMappingEntity = Result.responseBody(Response.RUNTIME_ERROR.getCode(), Response.RUNTIME_ERROR.getMsg(), e.getMessage());
		}
		return resultMappingEntity.toString();
	}
	
	
}
